package com.example.component;

import com.example.mapper.ShopMapper;
import com.example.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

/**
 * 缓存重建任务类——缓存雪崩问题：定时任务实现缓存预热
 */
//@Component
@Slf4j
public class CachePreheatTask {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ShopMapper shopMapper;

    // 执行缓存预热任务的方法
    @Scheduled(cron = "0 19 9 * * ?")
    public void preheatCache() {
        // 执行缓存预热逻辑
        List<Long> selectAllIds = shopMapper.selectAllIds();
        for (Long allId : selectAllIds) {
            //测试id=1，时间10s
            redisUtils.saveShop2Redis(allId, 60L);
            log.debug("缓存数据预热成功，id为：{}", allId);
        }
    }
}
